﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Timers;

namespace zmMoniter
{
    
    class receiveNeter
    {
        private string sIP = "";
        public int iPort;
       // private Server server = null;
        private IPEndPoint iep;
        private mainForm main = null;
        private TcpUtilClient client = null;
        private System.Timers.Timer userClientTimer;//维护客户端1对N连接
        public receiveNeter(string sStrIP, int sStrPort, mainForm frm)
        {        
            sIP = sStrIP;
            iPort = sStrPort;
            this.main = frm;

            iep = new IPEndPoint(IPAddress.Parse(sIP), iPort);


            System.Threading.Thread th = new Thread(new ThreadStart(this.DoWork));
            th.IsBackground = true;
            th.Start();

           

        }
        private void DoWork()
        {
            /*
           server = new Server(iep, true, 60000);
           server.connected += new ClientConnectedEventArgsHandler(server_connected);                    //客户端已连接事件
           server.clientCutConnection += new ClientCutConnectionEventHAndler(server_clientCutConnection); //客户端断开连接
           server.receiveMesage += new ServerReceiveMessageEventHander(server_receiveMesage);  //收到信息事件
              int iResult = server.Start();
             */
            client = new TcpUtilClient(iep, true, 60000);       
            client.receivMessage += new ClientReceiveMessageHander(client_receivMessage);        
            Int32 result = client.Connect();
            if (result == 1)
            {
                this.main.siInfo.Caption = "已建监控连接!";
                //维护软件客户端在线列表
                userClientTimer = new System.Timers.Timer(10000);
                userClientTimer.Elapsed += new ElapsedEventHandler(sendMesg);
                userClientTimer.Start();
            }
            else
            {
                this.main.siInfo.Caption = "连接服务器失败!";
            }

        }

        private void sendMesg(object sender, ElapsedEventArgs e)
        {
            try
            {
                byte[] buffer = Encoding.UTF8.GetBytes("03");
                // client.Connect();
                Int32 result = client.SendMesg(buffer);
                if (result == 1)
                {
                }
                else
                {
                    client.Connect();
                    client.SendMesg(buffer);
                }
            }
            catch
            {
                MessageBox.Show("请先连接服务器!");
            }
        }

        private void client_receivMessage(object sender, ClientReceiveMesageEventArgs e)
        {
           
            string mesg = Encoding.UTF8.GetString(e.Buffer);
            string[] msg = mesg.Split(';');
            if (msg.Length < 2)
                return;
            switch (msg[0])
            {
                case "01"://向客户端发送层变换指令
                    {
                        if (msg.Length < 3)
                            return;
                        string sDeckID = msg[2];
                        this.main.frEQ.upDateDeck(int.Parse(sDeckID)); 
                        break;
                    }
                case "02"://向客户端发送报警提示
                    {
                        if (msg.Length < 3)
                            return;
                        string sDeckID = msg[1];
                        string sWarnGUID = msg[2];
                        this.main.showWarning(sDeckID, sWarnGUID); 
                       // mainForm.publicFrom.showWarning(sDeckID, sWarnGUID); 
                        break;
                    }
                case "03"://向软件客户端发送振捣机位置
                    {
                        if (msg.Length < 6)
                            return;
                        string sDeckID = msg[2];
                        string sDeviceID = msg[1];
                        float fX = float.Parse(msg[3]);
                        float fY = float.Parse(msg[4]);
                        float fAngle = float.Parse(msg[5]);
                        this.main.frDeck2D.upDateDevicePos(sDeckID, sDeviceID, fX, fY, fAngle);
                        break;
                    }
                case "04"://向软件客户端发送振捣机完成事件
                    {
                        if (msg.Length < 3)
                            return;
                        string sDeckID = msg[1];
                        string sLayerName = msg[2];
                        this.main.frDeck2D.upDateLayer(sDeckID, sLayerName);
                        break;
                    }
                default:
                    break;
            }      
        }
        //private void server_connected(object sender, ClientConnectedEventArgs e)
        //{
           
        //}
        //private void server_clientCutConnection(object sender, ClientCutConnectionEventArgs e)
        //{
           
        //}

    }
}
